package com.sky.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sky.domain.User;
import com.sky.exception.UserException;
import com.sky.service.UserService;

public class UserInfoServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("===doGet==========");
		//1.获取请求参数
		String id = request.getParameter("id");
		System.out.println("=id=="+id);
		//2.处理业务逻辑
		UserService userService=new UserService();
		User user=null;
		try {
			// 根据ID查询用户
			user=userService.queryUserById(id);
			System.out.println(user.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("===22222222222222222==========");
			e.printStackTrace();
			request.setAttribute("error_msg01", e.getMessage());
			System.out.println("===3333333333333333==========");
		} 
		//3.分发转向
		if (user==null){
			List<User> list=null;
			try {
				 list = userService.queryUserList();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("list", list);
			request.getRequestDispatcher("home/showuserlist.jsp").forward(request, response);
		}else{
			request.setAttribute("user", user);		
					
			request.getRequestDispatcher("home/userinfo.jsp").forward(request, response);
		}
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}